//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    msgData: {
      birthday: "2000-01-01",
      calendar: 1,
      gender: 1,
      mobile: "",
      name: "",
      zcode: 86,
    },
    zcodes: [],
    index: 0
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.data.msgData.zcode = this.data.zcodes[e.detail.value].zcode
    this.setData({
      index: e.detail.value,
      msgData: this.data.msgData
    })
  },
  changeInput(e) {
    if (e.target.id == 'gender' || e.target.id == 'calendar') {
      this.data.msgData[e.target.id] = Number(e.detail.value)
    } else {
      this.data.msgData[e.target.id] = e.detail.value
    }
    this.setData({
      msgData: this.data.msgData
    })
  },
  addMembder() {
    app.$http('/members', this.data.msgData, 'post').then(response => {
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 2000
      })
      setTimeout(_ => {
        wx.navigateBack({
          delta: 1
        })
      }, 2000)
    }, error => {})
  },
  onShow() {
    app.$http('/zcodes').then(response => {
      this.setData({
        zcodes: response
      })
    }, error => {})
  }
})
